package 力扣;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author yyq
 * @create 2022-04-06 20:43
 */
public class leetcode515 {
    public List<Integer> largestValues(TreeNode root) {
        int LayerNodes = 1;
        Queue<TreeNode> queue=new LinkedList<>();
        List<Integer> list = new ArrayList<>();
        if(root==null) return list;
        queue.add(root);
        while (!queue.isEmpty()){
            int temp = 0;
            int max = queue.peek().val;
            while (LayerNodes!=0){
                TreeNode treeNode = queue.poll();
                int val = treeNode.val;
                if(val>max) max=val;
                if(treeNode.left!=null)
                {
                    queue.add(treeNode.left);
                    temp++;
                }
                if(treeNode.right!=null)
                {
                    queue.add(treeNode.right);
                    temp++;
                }
                LayerNodes--;
            }
            list.add(max);
            LayerNodes=temp;
        }
        return list;
    }
}
